package com.chuzhiyun.chengguoxy.feature.internship.clockin;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.TextView;
import com.chuzhiyun.chengguoxy.R;
import com.amap.api.location.AMapLocation;
import com.chuzhiyun.chengguoxy.feature.base.BaseFragment;
import com.chuzhiyun.chengguoxy.network.NetApi;
import com.chuzhiyun.chengguoxy.network.NetService;
import com.chuzhiyun.chengguoxy.network.StringNetCallback;
import com.chuzhiyun.chengguoxy.utlis.CustomClickListener;
import com.chuzhiyun.chengguoxy.utlis.LocationUtils;
import com.chuzhiyun.chengguoxy.utlis.PictureSelectUtil;
import com.chuzhiyun.chengguoxy.utlis.ToastCustom;
import com.google.gson.Gson;
import com.vondear.rxtool.RxFileTool;

import java.io.File;
import java.util.HashMap;
import java.util.Objects;

public class ClockInFragment extends BaseFragment<com.chuzhiyun.chengguoxy.databinding.FragmentClockInBinding> {

    private String longitude;
    private String latitude;
    private String street;
    private String adminDivision;
    private String mood;


    @Override
    public void initView(View view) {
        mood = "good";
        binding.navView.ivBack.setOnClickListener(new CustomClickListener() {
            @Override
            protected void onSingleClick() {
                requireActivity().finish();
            }
        });
        binding.navView.tvTitle.setText("打卡");
        binding.xinQing1.setOnClickListener(new CustomClickListener() {
            @Override
            protected void onSingleClick() {
                mood = "good";
                binding.xinQingText0.setTextColor(Color.parseColor("#FEC43C"));
                binding.xinQingText1.setTextColor(Color.parseColor("#2C2C2C"));
                binding.xinQingText2.setTextColor(Color.parseColor("#2C2C2C"));
            }
        });
        binding.xinQing2.setOnClickListener(new CustomClickListener() {
            @Override
            protected void onSingleClick() {
                mood = "moderate";
                binding.xinQingText1.setTextColor(Color.parseColor("#FEC43C"));
                binding.xinQingText0.setTextColor(Color.parseColor("#2C2C2C"));
                binding.xinQingText2.setTextColor(Color.parseColor("#2C2C2C"));
            }
        });
        binding.xinQing3.setOnClickListener(new CustomClickListener() {
            @Override
            protected void onSingleClick() {
                mood = "bad";
                binding.xinQingText2.setTextColor(Color.parseColor("#FEC43C"));
                binding.xinQingText0.setTextColor(Color.parseColor("#2C2C2C"));
                binding.xinQingText1.setTextColor(Color.parseColor("#2C2C2C"));
            }
        });
    }

    @Override
    public void initData() {
        try {
            LocationUtils.getInstance().getLocation(getContext(), new LocationUtils.OnLocationChangedListener() {
                @SuppressLint("DefaultLocale")
                @Override
                public void onSuccess(AMapLocation amapLocation) {
                    longitude = String.format("%.6f",amapLocation.getLongitude());
                    latitude = String.format("%.6f",amapLocation.getLatitude());
                    adminDivision = amapLocation.getAdCode()+"";
                    street = amapLocation.getAoiName();
                }

                @Override
                public void onFail(int errCode, String errInfo) {
                    System.out.println(errInfo);
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    @Override
    public void setListener() {
        binding.clockInNameText.setText("打卡");
        binding.clockInLl.setOnClickListener(new CustomClickListener() {
            @Override
            protected void onSingleClick() {
                showCameraAlert();
            }
        });
    }

    private void clockInClick(){
        String url = NetApi.postPunches();
        HashMap<String,String>param = new HashMap<>();
        param.put("latitude",latitude);
        param.put("longitude",longitude);
        param.put("street",street);
        param.put("adminDivision",adminDivision);
        param.put("mood",mood);

        Gson gson = new Gson();
        String content = gson.toJson(param);
        showLoadingDialog();

        NetService.getInstance().postJson(url, content, new StringNetCallback() {
            @Override
            public void onSuccess(String response, int statusCode) throws Exception {
                dismissLoadingDialog();
                if (statusCode==201){
                    ToastCustom.showSuccessToast("打卡成功");
                }
            }

            @Override
            public void onError(String error) {
                dismissLoadingDialog();
            }
        });

    }

    private void showCameraAlert(){
        Dialog dialog = new Dialog(requireContext());
        @SuppressLint("InflateParams") View view = LayoutInflater.from(getContext()).inflate(R.layout.dialog_clockinphoto, null);
        dialog.setContentView(view);
        Window window = dialog.getWindow();
        // 设置显示视图内容
        assert window != null;
        window.setContentView(view);
        window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        dialog.show();

        TextView sureText = dialog.findViewById(R.id.sure_Text);
        sureText.setOnClickListener(new CustomClickListener() {
            @Override
            protected void onSingleClick() {
                dialog.dismiss();
                takePhotoCamera();
            }
        });

        TextView cancelText = dialog.findViewById(R.id.cancel_Text);
        cancelText.setOnClickListener(new CustomClickListener() {
            @Override
            protected void onSingleClick() {
                dialog.dismiss();
            }

        });
    }

    private void takePhotoCamera(){
        PictureSelectUtil.with(this)
                .camera()
                .setCallback(new PictureSelectUtil.OnCallback() {
                    @Override
                    public void onCallback(Uri uri) {
                        File mFile = RxFileTool.getFilePhotoFromUri(getActivity(),uri);
                        clockInCamera(mFile);
                    }
                }).select();
    }

    private void clockInCamera(File file){
        String url = NetApi.postPunches();
        HashMap<String,String>param = new HashMap<>();
        param.put("latitude",latitude);
        param.put("longitude",longitude);
        param.put("street",street);
        param.put("adminDivision",adminDivision);
        param.put("mood","good");
        showLoadingDialog();
        NetService.getInstance().postPunchWithParam(url, param, file, new StringNetCallback() {
            @Override
            public void onSuccess(String response, int statusCode) throws Exception {
                dismissLoadingDialog();
                if (statusCode==201){
                    ToastCustom.showSuccessToast("打卡成功");

                }
            }

            @Override
            public void onError(String error) {
                dismissLoadingDialog();
            }
        });
    }

}